package com.czn.shopdemo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czn.shopdemo.config.MinioConfig;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.model.vo.GoodsVO;
import com.czn.shopdemo.service.GoodsService;
import com.czn.shopdemo.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Zenan Chen
 * @date 2023/4/3 10:52
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);

    @ApiOperation("商品上线/下线")
    @PostMapping("/onLine")
    public Result onLine(@RequestParam String uuid, @RequestParam(defaultValue = "1") String onLine) {
        try {
            log.info("接受到参数：onLine:{}", onLine);
            log.info("------------------------->onLine 商品上线/下线开始");
            goodsService.onLine(uuid,onLine);
            log.info("------------------------->onLine 商品上线/下线结束");
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error("------------------------->onLine 商品上线/下线异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    /**
     * 上传商品图片到minio
     *
     * @param file 文件
     * @return
     */
    @ApiOperation(value = "上传商品图片到minio", notes = "上传商品图片到minio")
    @RequestMapping(value = "uploadImg", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    @ResponseBody
    public Object uploadImg(@ApiParam(value = "上传的文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file) {
        try {
            log.info("-------------------------------------> 上传商品图片到minio开始");
            String url = goodsService.uploadImg(file);
            log.info("-------------------------------------> 上传商品图片到minio结束");
            return Result.ok(url);
        } catch (Exception e) {
            log.error("------------------------->/saveGoods 上传商品图片到minio异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("获取商品详情")
    @PostMapping("/getGoodDetail")
    public Result getGoodDetail(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/getGoodDetail 获取商品详情开始");
            Goods goods = goodsService.getGoodDetail(uuid);
            log.info("------------------------->/getGoodDetail 获取商品详情结束");
            return Result.ok(goods);
        } catch (Exception e) {
            log.error("------------------------->/getGoodDetail -获取商品详情异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("保存商品")
    @PostMapping("/saveGoods")
    public Result save(@RequestBody Goods goods, HttpServletRequest request) {
        try {
            log.info("接受到参数：goods:{}", JSONObject.toJSONString(goods));
            log.info("------------------------->/saveGoods 保存商品开始");
            goodsService.saveGoods(goods, request);
            log.info("------------------------->/saveGoods 保存商品结束");
            return Result.ok("保存商品成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveGoods 保存商品异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("删除商品")
    @PostMapping("/deleteGoods")
    public Result deleteGoods(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/saveGoods 删除商品开始");
            goodsService.deleteGoods(uuid);
            log.info("------------------------->/saveGoods 删除商品结束");
            return Result.ok("删除商品成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveGoods 删除商品异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("查询商品列表")
    @PostMapping("/getGoodsList")
    public Result getGoodsList(@RequestParam Integer pageNo,
                               @RequestParam Integer pageSize,
                               @RequestBody GoodsVO goodsVO) {
        try {
            log.info("接受到参数：page:{},limit:{},goodsVO:{}", pageNo, pageSize, JSONObject.toJSONString(goodsVO));
            log.info("------------------------->/getGoodsList/{page}/{limit} 查询商品列表开始");
            Page<Goods> result = goodsService.getGoodsList(pageNo, pageSize, goodsVO);
            log.info("------------------------->/getGoodsList/{page}/{limit} 查询商品列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/getGoodsList/{page}/{limit} 查询商品列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

}
